package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {

    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main........start..........");
        //无返回值
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程号：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运算结果：" + i);
//        }, executor);

        //有返回值
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程号：" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运算结果：" + i);
//            return i;
//        }, executor).whenComplete((res,exception) -> {
//            //虽然能得到异常信息，但是没办法修改返回数据。
//            System.out.println("异步任务完成了.....结果是:"+res+";异常是："+exception);
//        }).exceptionally(throwable -> {
//            //可以感知异常，同时返回默认值
//            return 10;
//        });

        //方法完成后的处理
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程号：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运算结果：" + i);
//            return i;
//        }, executor).handle((res,thr) -> {
//            if (res != null) {
//                return res*2;
//            }
//            if (thr != null) {
//                return 0;
//            }
//            return 0;
//        });
//        Integer integer = future.get();
//        System.out.println("main........end.........."+ integer);

//        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务1结束：");
//            return i;
//        }, executor);
//
//        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2线程：" + Thread.currentThread().getId());
//            try {
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("任务2结束：");
//            return "Hello";
//        }, executor);

//        future01.runAfterBothAsync(future02,() -> {
//            System.out.println("任务3开始......");
//        }, executor);

//        future01.thenAcceptBothAsync(future02,(f1,f2) -> {
//            System.out.println("任务3开始.....之前的结果："+f1+"------>"+f2);
//        }, executor);

//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + ":" + f2 + "---->Haha";
//        }, executor);
        /**
         * 两个任务只要有一个完成，我们就执行任务3
         * runAfterEitherAsync：不感知结果，自己没有返回值
         * acceptEitherAsync：感知结果，自己没有返回值
         */
//        future01.runAfterEitherAsync(future02,() -> {
//            System.out.println("任务3开始.....之前的结果：");
//        }, executor);
//
//        future01.acceptEitherAsync(future02,(res) -> {
//            System.out.println("任务3开始....之前的结果："+res);
//        },executor);
//
//        CompletableFuture<String> future = future01.applyToEitherAsync(future02, res -> {
//            System.out.println("任务3开始....之前的结果：" + res);
//            return res.toString() + "--->哈哈";
//        }, executor);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        }, executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性信息");
            return "黑色+256G";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品介绍");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        }, executor);

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
//        allOf.get();    //等待所有结果完成
//        System.out.println(futureImg.get()+"---->"+futureAttr.get()+"---->"+futureDesc.get());
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        System.out.println(anyOf.get());
        System.out.println("main........end..........");
    }

    public void Thread(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main........start..........");
        //1、继承Thread
        Thread01 thread01 = new Thread01();
        thread01.start();

        //2、实现Runnable接口
        Runnable01 runnable01 = new Runnable01();
        new Thread(runnable01).start();

        //3、实现Callable接口+ FutureTask(可以拿到返回结果，可以处理异常)
        FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
        new Thread(futureTask).start();
        //阻塞等待整个线程执行完成，获取返回结果
        Integer integer = futureTask.get();
        System.out.println("FutureTask计算返回结果：" + integer);

        //4、线程池：给线程池直接提交任务
        //======>我们以后的业务代码里面，以上三种启动线程的方式都不用，【将所有的多线程异步任务都交给线程池执行】
        //当前系统中线程池只有一两个,每个异步任务，提交给线程池让他自己去执行
        //service.execute(new Runnable01());


        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                200,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        System.out.println("main........end..........");
    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程号：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运算结果：" + i);
        }
    }

    public static class Runnable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程号：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运算结果：" + i);
        }
    }

    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程号：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运算结果：" + i);
            return i;
        }
    }
}
